package com.peakmain.wanandroid.mymodule.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.peakmain.baselibrary.adapter.BaseViewPagerAdapter;
import com.peakmain.baselibrary.imageLoader.ImageLoader;
import com.peakmain.minemodule.R;

import java.util.List;

/**
 * author ：Peakmain
 * createTime：2020/2/22
 * mail:2726449200@qq.com
 * describe：
 */
public class ImagePreviewAdpater extends BaseViewPagerAdapter {
    private Context mContext;
    private List<String> mImageList;


    public ImagePreviewAdpater(Context context, List<String> imageList) {
        this.mContext = context;
        this.mImageList = imageList;
    }

    public List<String> getImageList() {
        return mImageList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        PhotoView photoView = new PhotoView(mContext);
        String url = getImageUrl(position);
        ImageLoader.getInstance().displayImage(mContext, url, photoView, R.drawable.ic_about);
        photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(ImageView view, float x, float y) {
                if (mOnPreviewImageClick != null) {
                    mOnPreviewImageClick.onImageClick(view);
                }
            }
        });
        photoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (mOnPreviewImageLongClick != null) {
                    mOnPreviewImageLongClick.onImageLongCick(view);
                }
                return false;
            }
        });
        return photoView;
    }

    private String getImageUrl(int position) {
        return mImageList != null ? mImageList.get(position) : "";
    }

    public void removeImage(int position) {
        if (mImageList != null) {
            mImageList.remove(position);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return mImageList != null ? mImageList.size() : 0;
    }

    private onPreviewImageClick mOnPreviewImageClick;

    public void setOnPreviewImageClick(onPreviewImageClick onPreviewImageClick) {
        mOnPreviewImageClick = onPreviewImageClick;
    }

    public interface onPreviewImageClick {

        void onImageClick(View view);
    }

    private onPreviewImageLongClick mOnPreviewImageLongClick;

    public interface onPreviewImageLongClick {
        void onImageLongCick(View view);
    }

    public void setOnPreviewImageLongClick(onPreviewImageLongClick onPreviewImageLongClick) {
        mOnPreviewImageLongClick = onPreviewImageLongClick;
    }
}
